import 'package:after_sale_plugin/pages/models/vehicle_err_back_model.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import '../../../../service/global_service.dart';
import '../../../models/vehicle_error_model.dart';
import 'controller.dart';

class VehicleErrorView extends BaseGetView<VehicleErrorController> {
  VehicleErrorView({super.key});

  @override
  String get title => local.vehicle_faults;

  @override
  Widget? get backView => CommonWidgets.appBarLeading(
        onTap: () {
          Get.back(
              result: AfterSalePluginBackModel()
                ..errorList.value = controller.state.errorList
                    .map<Map>((errorModel) => errorModel.toMap())
                    .toList());
        },
      );

  @override
  Widget buildBody(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Column(
        children: [
          // 统计信息卡片
          _buildStatsCard(),
          const SizedBox(height: 8),
          // 错误列表
          Expanded(
            child: Obx(
              () {
                final errorList =
                    GlobalService.to.realtimeError.value.vehicleErrors;
                return errorList.isEmpty
                    ? _buildEmptyState()
                    : ListView.builder(
                        itemCount: errorList.length,
                        itemBuilder: (context, index) {
                          final error = errorList[index];
                          return _buildErrorItem(error);
                        },
                      );
              },
            ),
          ),
        ],
      ),
    );
  }

  // 构建统计信息卡片
  Widget _buildStatsCard() {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Obx(() {
          final errorList = GlobalService.to.realtimeError.value.vehicleErrors;
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              _buildStatItem(
                local.critical,
                errorList
                    .where((e) => e.type == VehicleErrorType.error)
                    .length
                    .toString(),
                Colors.red,
              ),
              _buildStatItem(
                local.warning,
                errorList
                    .where((e) => e.type == VehicleErrorType.warning)
                    .length
                    .toString(),
                Colors.orange,
              ),
              _buildStatItem(
                local.protection,
                errorList
                    .where((e) => e.type == VehicleErrorType.protected)
                    .length
                    .toString(),
                Colors.blue,
              ),
            ],
          );
        }),
      ),
    );
  }

  // 构建统计项
  Widget _buildStatItem(String title, String value, Color color) {
    return Column(
      children: [
        Text(
          value,
          style: TextStyle(
            fontSize: 20,
            fontWeight: FontWeight.bold,
            color: color,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          title,
          style: TextStyle(
            fontSize: 12,
            color: Colors.grey[600],
          ),
        ),
      ],
    );
  }

  // 构建空状态
  Widget _buildEmptyState() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            Icons.check_circle_outline,
            size: 64,
            color: Colors.green[300],
          ),
          const SizedBox(height: 16),
          Text(
            local.no_vehicle_fault,
            style: TextStyle(
              fontSize: 18,
              color: Colors.grey[600],
            ),
          ),
          const SizedBox(height: 8),
          Text(
            local.vehicle_normal,
            style: TextStyle(
              fontSize: 14,
              color: Colors.grey[500],
            ),
          ),
        ],
      ),
    );
  }

  // 构建错误列表项
  Widget _buildErrorItem(VehicleErrorModel error) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
      child: ListTile(
        leading: Icon(
          Icons.error,
          color: error.type?.color ?? Colors.grey,
        ),
        title: Text(
          error.message ?? "",
          style: TextStyle(
            fontWeight: FontWeight.w500,
            color: Colors.grey[400],
          ),
        ),
      ),
    );
  }
}
